﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
using BaseSet.Framework.Util;

namespace WebSite.UserSetting
{
    public partial class UserBrief : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent();
            }
        }

        #region 保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string fileFullName = "";
            string newfilename = "";
            string filePath = "";
            string fileFolder = "/Files/headphoto/";

            string originalImagePath = "";
            string thumbnailPath = "";

            if (fuFile.HasFile)
            {
                //判断文件的大小
                string FileName = fuFile.FileName.ToString();
                string ExtenName = System.IO.Path.GetExtension(FileName);//获取扩展名
                if (ExtenName.ToLower() == ".jpg" || ExtenName.ToLower() == ".png" || ExtenName.ToLower() == ".bmp" || ExtenName.ToLower() == ".jpeg" || ExtenName.ToLower() == ".gif")
                {
                    string serverPath = "..\\Files\\headphoto\\";
                    filePath = Server.MapPath(serverPath);
                    string newfilepath = filePath;

                    if (!System.IO.Directory.Exists(filePath))
                    {
                        System.IO.Directory.CreateDirectory(filePath);
                    }

                    string filename = DateTime.Now.ToString("yyyyMMddhhmmss");

                    fileFullName = filename + ExtenName;
                    newfilename = filename+"new" + ExtenName;
                    filePath += fileFullName;
                    newfilepath += newfilename;
                    fuFile.SaveAs(filePath);

                    //压缩图片
                    originalImagePath = filePath;
                    thumbnailPath = newfilepath;

                    int width = 150;
                    int height = 150;
                    string mode = "Cut";
                    string imageType = "";
                    if (ExtenName.ToLower() == ".gif")
                    {
                        imageType = ".gif";
                    }
                    else
                    {
                        imageType = ".jpg";
                    }
                    ImageUtils.MakeThumbnail(originalImagePath, thumbnailPath, width, height, mode, imageType);
                    //将原图片删除
                    System.IO.File.Delete(filePath);
                }
                else
                {
                    Tools.Alert("图片格式不正确");
                    return;
                }
            }

            string brief = txtBrief.Text.Trim();

            Users uinfo = UserBase.GetCurrentUser();
            if (!string.IsNullOrEmpty(thumbnailPath))
            {
                uinfo.SmallHeadImage = fileFolder + newfilename;
            }
            uinfo.Brief = brief;
            UsersHelper uhelper = new UsersHelper();
            uhelper.EditUsers(uinfo);

            Tools.AlertAndRedirect("个人设置成功", "UserBrief.aspx");
            return;
        }
        #endregion

        #region 加载内容
        private void LoadContent()
        {
            Users uinfo = UserBase.GetCurrentUser();
            string brief = uinfo.Brief;
            txtBrief.Text = brief;
            imgHead.ImageUrl = uinfo.SmallHeadImage;
        }
        #endregion
    }
}